#include "CCamera.h"

#include "../gameobjects/CBaseObject.h"

CCamera::CCamera()
{
	m_pFollowingObject = NULL;
	memset(&m_CameraOffset, 0, sizeof(POINT));
}

CCamera::~CCamera()
{
}

void CCamera::Initialize(int  nViewWidth, int nViewHeight, CBaseObject* pObject)
{
	m_nWidth = nViewWidth >> 1;
	m_nHeight = nViewHeight >> 1;
	m_pFollowingObject = pObject;
}

void CCamera::Update(float fDeltaTime)
{
	if(m_pFollowingObject != NULL)
	{
		const POINT& objPos = m_pFollowingObject->GetPosition();
		m_CameraOffset.x = objPos.x - m_nWidth;
		m_CameraOffset.y = objPos.y - m_nHeight;
	}
	m_ViewRect.left = m_CameraOffset.x;
	m_ViewRect.top = m_CameraOffset.y;
	m_ViewRect.right = m_ViewRect.left + (m_nWidth << 1);
	m_ViewRect.bottom = m_ViewRect.top + (m_nHeight << 1);
}

void CCamera::Shutdown()
{
	m_pFollowingObject = NULL;
}

void CCamera::AttachObject(CBaseObject* pObject)
{
	m_pFollowingObject = pObject;
}

bool CCamera::RectOnScreen(RECT& rect)
{
	RECT out;
	BOOL result = IntersectRect(&out, &m_ViewRect, &rect);
	return result != 0;
}